#! /bin/bash

if [ -n "$2" ] ;
then
  do_date=$2
else
  do_date=`date -d '-1 day' +%F`
fi

env_config="
use dw;
SET mapreduce.job.queuename=tianyan;
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
SET hive.exec.dynamic.partition.mode=nonstrict;"

dwd_company_rebate_detail="
insert into table dwd_company_rebate_detail partition(dt)
select id
,rebate_period_type
,recharge_id
,order_id
,release_id
,rule_id
,period_id
,trade_type
,consume_type
,group_id
,company_id
,station_id
,p_cart_id
,cart_id
,c_discount_money
,c_pay_money
,mc_discount_money
,mc_pay_money
,total_money
,order_create_time
,fuel_no
,quantity
,related_amt
,rebate_type
,rebate_money
,create_time
,update_time
,SUBSTRING(FROM_UNIXTIME(order_create_time),1,10)
from ods_rebate_detail ord
where business_type = 1
and ord.dt = '$do_date';"

dwd_station_rebate_detail="
insert into dw.dwd_station_rebate_detail partition (odt)
select
       rebate_period_type,
       recharge_id,
       order_id,
       release_id,
       rule_id,
       period_id,
       trade_type,
       consume_type,
       group_id,
       company_id,
       station_id,
       p_cart_id,
       cart_id,
       c_discount_money,
       c_pay_money,
       mc_discount_money,
       mc_pay_money,
       total_money,
       order_create_time,
       fuel_no,
       quantity,
       related_amt,
       rebate_type,
       if(rebate_money != null, rebate_money, 0) as rebate_money,
       create_time,
       update_time,
       SUBSTRING(FROM_UNIXTIME(order_create_time),1,10) as odt
from dw.ods_rebate_detail ord
where business_type = 2 and ord.dt = '$do_date';
"

insert_into_table(){
  case $1 in
  "all")
    echo $1
    hive -e "$env_config$dwd_company_rebate_detail$dwd_station_rebate_detail"
    ;;
  "dwd_company_rebate_detail")
    echo $1
    hive -e "$env_config$dwd_company_rebate_detail"
    ;;
  "dwd_station_rebate_detail")
    hive -e "$env_config$dwd_station_rebate_detail"
    ;;
  esac
}

insert_into_table $1